package com.business.dynamic.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.business.common.dto.R;
import com.business.common.utils.StrKit;
import com.business.dynamic.entity.DynamicContentTemplate;
import com.business.dynamic.mapper.DynamicContentTemplateMapper;
import com.business.dynamic.service.DynamicContentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 内容模板表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-11-29 15:15:38
 * @since 1.0
 */
@Service
public class DynamicContentTemplateServiceImpl implements DynamicContentTemplateService {

    @Autowired
    private DynamicContentTemplateMapper dynamicContentTemplateMapper;


    @Override
    public DynamicContentTemplate query(Integer id) {
        DynamicContentTemplate dynamicContentTemplate = dynamicContentTemplateMapper.selectById(id);
        return dynamicContentTemplate;
    }

    @Override
    public List<DynamicContentTemplate> queryList(Integer type, String parentCategoryCode, String subCategoryCode) {
        Wrapper<DynamicContentTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        if (StrKit.isNotEmpty(subCategoryCode)) {
            wrapper.eq("sub_category_code", subCategoryCode);
        } else {
            wrapper.eq("parent_category_code", parentCategoryCode);
        }
        List<DynamicContentTemplate> result = dynamicContentTemplateMapper.selectList(wrapper);
        //如果按子行业查询没有则查夫行业
        if ((result == null || result.size() == 0) && StrKit.isNotEmpty(subCategoryCode)) {
            return queryList(type, parentCategoryCode, null);
        }
        return result;
    }


}